//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.goldhuman.Common;

import com.goldhuman.Common.Marshal.MarshalException;
import com.goldhuman.Common.Marshal.OctetsStream;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;

public class Octets implements Cloneable, Comparable<Object>, Serializable {
    private static final long serialVersionUID = -6683312389154167084L;
    private static final int DEFAULT_SIZE = 128;
    private static String DEFAULT_CHARSET = "ISO-8859-1";
    private byte[] buffer = null;
    private int count = 0;

    private byte[] roundup(int size) {
        int capacity;
        for(capacity = 16; size > capacity; capacity <<= 1) {
        }

        return new byte[capacity];
    }

    public void reserve(int size) {
        if (this.buffer == null) {
            this.buffer = this.roundup(size);
        } else if (size > this.buffer.length) {
            byte[] tmp = this.roundup(size);
            System.arraycopy(this.buffer, 0, tmp, 0, this.count);
            this.buffer = tmp;
        }

    }

    public Octets replace(byte[] data, int pos, int size) {
        this.reserve(size);
        System.arraycopy(data, pos, this.buffer, 0, size);
        this.count = size;
        return this;
    }

    public Octets replace(Octets data, int pos, int size) {
        return this.replace(data.buffer, pos, size);
    }

    public Octets replace(byte[] data) {
        return this.replace((byte[])data, 0, data.length);
    }

    public Octets replace(Octets data) {
        return this.replace((byte[])data.buffer, 0, data.count);
    }

    public Octets() {
        this.reserve(128);
    }

    public Octets(int size) {
        this.reserve(size);
    }

    public Octets(Octets rhs) {
        this.replace(rhs);
    }

    public Octets(byte[] rhs) {
        this.replace(rhs);
    }

    private Octets(byte[] bytes, int length) {
        this.buffer = bytes;
        this.count = length;
    }

    public static Octets wrap(byte[] bytes, int length) {
        return new Octets(bytes, length);
    }

    public static Octets wrap(byte[] bytes) {
        return wrap(bytes, bytes.length);
    }

    public static Octets wrap(String str, String encoding) {
        try {
            return wrap(str.getBytes(encoding));
        } catch (UnsupportedEncodingException var3) {
            throw new RuntimeException(var3);
        }
    }

    public Octets(byte[] rhs, int pos, int size) {
        this.replace(rhs, pos, size);
    }

    public Octets(Octets rhs, int pos, int size) {
        this.replace(rhs, pos, size);
    }

    public Octets resize(int size) {
        this.reserve(size);
        this.count = size;
        return this;
    }

    public int size() {
        return this.count;
    }

    public int capacity() {
        return this.buffer.length;
    }

    public Octets clear() {
        this.count = 0;
        return this;
    }

    public Octets swap(Octets rhs) {
        int size = this.count;
        this.count = rhs.count;
        rhs.count = size;
        byte[] tmp = rhs.buffer;
        rhs.buffer = this.buffer;
        this.buffer = tmp;
        return this;
    }

    public Octets push_back(byte data) {
        this.reserve(this.count + 1);
        this.buffer[this.count++] = data;
        return this;
    }

    public Octets erase(int from, int to) {
        System.arraycopy(this.buffer, to, this.buffer, from, this.count - to);
        this.count -= to - from;
        return this;
    }

    public Octets insert(int from, byte[] data, int pos, int size) {
        this.reserve(this.count + size);
        System.arraycopy(this.buffer, from, this.buffer, from + size, this.count - from);
        System.arraycopy(data, pos, this.buffer, from, size);
        this.count += size;
        return this;
    }

    public Octets insert(int from, Octets data, int pos, int size) {
        return this.insert(from, data.buffer, pos, size);
    }

    public Octets insert(int from, byte[] data) {
        return this.insert(from, (byte[])data, 0, data.length);
    }

    public Octets insert(int from, Octets data) {
        return this.insert(from, (byte[])data.buffer, 0, data.size());
    }

    public Object clone() {
        return new Octets(this);
    }

    public int compareTo(Octets rhs) {
        int c = this.count - rhs.count;
        if (c != 0) {
            return c;
        } else {
            byte[] v1 = this.buffer;
            byte[] v2 = rhs.buffer;

            for(int i = 0; i < this.count; ++i) {
                int v = v1[i] - v2[i];
                if (v != 0) {
                    return v;
                }
            }

            return 0;
        }
    }

    public int compareTo(Object o) {
        return this.compareTo((Octets)o);
    }

    public boolean equals(Object o) {
        if (this == o) {
            return true;
        } else {
            return this.compareTo(o) == 0;
        }
    }

    public int hashCode() {
        if (this.buffer == null) {
            return 0;
        } else {
            int result = 1;

            for(int i = 0; i < this.count; ++i) {
                result = 31 * result + this.buffer[i];
            }

            return result;
        }
    }

    public String toString() {
        return "octets.size=" + this.count;
    }

    public byte[] getBytes() {
        byte[] tmp = new byte[this.count];
        System.arraycopy(this.buffer, 0, tmp, 0, this.count);
        return tmp;
    }

    public byte[] array() {
        return this.buffer;
    }

    public byte getByte(int pos) {
        return this.buffer[pos];
    }

    public void setByte(int pos, byte b) {
        this.buffer[pos] = b;
    }

    public ByteBuffer getByteBuffer(int off, int size) {
        return ByteBuffer.wrap(this.buffer, off, size);
    }

    public ByteBuffer getByteBuffer(int off) {
        return ByteBuffer.wrap(this.buffer, off, this.count - off);
    }

    public ByteBuffer getByteBuffer() {
        return ByteBuffer.wrap(this.buffer, 0, this.count);
    }

    public String getString() throws Exception {
        return new String(this.buffer, 0, this.count, DEFAULT_CHARSET);
    }

    public String getString(String encoding) {
        try {
            return new String(this.buffer, 0, this.count, encoding);
        } catch (UnsupportedEncodingException var3) {
            throw new RuntimeException(var3);
        }
    }

    public void setString(String str) throws Exception {
        this.buffer = str.getBytes(DEFAULT_CHARSET);
        this.count = this.buffer.length;
    }

    public void dump() {
        for(int i = 0; i < this.size(); ++i) {
            System.out.printf("%02x ", this.buffer[i]);
        }

        System.out.printf("\n");
    }

    public static void setDefaultCharset(String name) {
        DEFAULT_CHARSET = name;
    }

    public static void main(String[] arg) {
        Octets x = new Octets("ddd".getBytes());
        Octets y = new Octets("ABC\r\n".getBytes());
        x.insert(x.size(), y);
        System.out.println(new String(x.getBytes()));
        System.out.println((new String(x.getBytes())).trim());
        String aa = "行为123";

        try {
            Octets oo = new Octets(aa.getBytes("UTF-16LE"));
            System.out.println(oo.getString("UTF-16LE"));
        } catch (Exception var12) {
        }

        try {
            OctetsStream info = new OctetsStream();
            String name = new String("和速度血魔血魔22".getBytes("UTF-16LE"));
            Octets name_os = new Octets(name.getBytes("UTF-16LE"));
            System.out.println(name);
            name_os.dump();
            info.marshal(name_os);
            info.dump();
            String info_gbk = info.getString("UTF-16LE");
            (new Octets(info_gbk.getBytes("UTF-16LE"))).dump();
            OctetsStream info_gbk_os = new OctetsStream(new Octets(info_gbk.getBytes("UTF-16LE")));

            try {
                info_gbk_os.dump();
                Octets new_name = new Octets();
                info_gbk_os.unmarshal(new_name);
                new_name.dump();
                System.out.println(new_name.getString("UTF-16LE"));
                System.out.println(new_name.getString("GBK"));
                System.out.println(new_name.getString("UTF-8"));
            } catch (MarshalException var10) {
                var10.printStackTrace();
            }
        } catch (UnsupportedEncodingException var11) {
            var11.printStackTrace();
        }

    }
}
